package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	fmt.Println(
		convertTobin(5),
		convertTobin(15),
		convertTobin(105),
		convertTobin(0),
		convertTobin(1),
		convertTobin(2),
	)

	printFile("go.mod")
	forever()
}

func forever() {
	for {
		fmt.Println("forever")
	}
}

func printFile(filename string) {
	if file, err := os.Open(filename); err != nil {
		panic(err)
	} else {
		scanner := bufio.NewScanner(file)
		//类似于while
		for scanner.Scan() {
			fmt.Println("scanner read:", scanner.Text())
		}
	}
}

func convertTobin(n int) string {
	if n == 0 {
		return "0"
	}
	var result = ""
	for ; n > 0; n /= 2 {
		lsb := n % 2
		result = strconv.Itoa(lsb) + result
	}
	return result
}
